package fr.xebia.monitoring.jdbc;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;

import fr.xebia.monitoring.Monitor;
import fr.xebia.monitoring.MonitorFactory;

public class MonitoredPreparedStatement extends MonitoredStatement implements PreparedStatement {

	private PreparedStatement preparedStatement;
//	private String rootMonitoringKey;
	private String monitoringKey;

	public MonitoredPreparedStatement(String key, PreparedStatement stmt) {
		super(key, stmt);
		this.preparedStatement = stmt;
//		this.rootMonitoringKey = key;
		this.monitoringKey = key+".preparedStatement";
	}

	public void addBatch() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "addBatch()");
		try {
			this.preparedStatement.addBatch();
		} finally {
			monitor.stop();
		}
	}

	public void clearParameters() throws SQLException {
		this.preparedStatement.clearParameters();
	}

	public boolean execute() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "execute()");
		try {
			return this.preparedStatement.execute();
		} finally {
			monitor.stop();
		}
	}

	public ResultSet executeQuery() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeQuery()");
		try {
			return this.preparedStatement.executeQuery();
		} finally {
			monitor.stop();
		}
	}

	public int executeUpdate() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeUpdate()");
		try {
			return this.preparedStatement.executeUpdate();
		} finally {
			monitor.stop();
		}
	}

	@Override
	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "execute()");
		try {
			return this.preparedStatement.execute(sql, autoGeneratedKeys);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "execute()");
		try {
			return this.preparedStatement.execute(sql, columnIndexes);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public boolean execute(String sql, String[] columnNames) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "execute()");
		try {
			return this.preparedStatement.execute(sql, columnNames);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public boolean execute(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "execute()");
		try {
			return this.preparedStatement.execute(sql);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public int[] executeBatch() throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeBatch()");
		try {
			return this.preparedStatement.executeBatch();
		} finally {
			monitor.stop();
		}
	}

	@Override
	public ResultSet executeQuery(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeQuery()");
		try {
			return this.preparedStatement.executeQuery(sql);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeUpdate()");
		try {
			return this.preparedStatement.executeUpdate(sql, autoGeneratedKeys);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeUpdate()");
		try {
			return this.preparedStatement.executeUpdate(sql, columnIndexes);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeUpdate()");
		try {
			return this.preparedStatement.executeUpdate(sql, columnNames);
		} finally {
			monitor.stop();
		}
	}

	@Override
	public int executeUpdate(String sql) throws SQLException {
		Monitor monitor = MonitorFactory.start(monitoringKey, "executeUpdate()");
		try {
			return this.preparedStatement.executeUpdate(sql);
		} finally {
			monitor.stop();
		}
	}

	public ResultSetMetaData getMetaData() throws SQLException {
		return this.preparedStatement.getMetaData();
	}

	public ParameterMetaData getParameterMetaData() throws SQLException {
		return this.preparedStatement.getParameterMetaData();
	}

	public void setArray(int i, Array x) throws SQLException {
		this.preparedStatement.setArray(i, x);
	}

	public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
		this.preparedStatement.setAsciiStream(parameterIndex, x, length);
	}

	public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException {
		this.preparedStatement.setBigDecimal(parameterIndex, x);
	}

	public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException {
		this.preparedStatement.setBinaryStream(parameterIndex, x, length);
	}

	public void setBlob(int i, Blob x) throws SQLException {
		this.preparedStatement.setBlob(i, x);
	}

	public void setBoolean(int parameterIndex, boolean x) throws SQLException {
		this.preparedStatement.setBoolean(parameterIndex, x);
	}

	public void setByte(int parameterIndex, byte x) throws SQLException {
		this.preparedStatement.setByte(parameterIndex, x);
	}

	public void setBytes(int parameterIndex, byte[] x) throws SQLException {
		this.preparedStatement.setBytes(parameterIndex, x);
	}

	public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
		this.preparedStatement.setCharacterStream(parameterIndex, reader, length);
	}

	public void setClob(int i, Clob x) throws SQLException {
		this.preparedStatement.setClob(i, x);
	}

	public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException {
		this.preparedStatement.setDate(parameterIndex, x, cal);
	}

	public void setDate(int parameterIndex, Date x) throws SQLException {
		this.preparedStatement.setDate(parameterIndex, x);
	}

	public void setDouble(int parameterIndex, double x) throws SQLException {
		this.preparedStatement.setDouble(parameterIndex, x);
	}

	public void setFloat(int parameterIndex, float x) throws SQLException {
		this.preparedStatement.setFloat(parameterIndex, x);
	}

	public void setInt(int parameterIndex, int x) throws SQLException {
		this.preparedStatement.setInt(parameterIndex, x);
	}

	public void setLong(int parameterIndex, long x) throws SQLException {
		this.preparedStatement.setLong(parameterIndex, x);
	}

	public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException {
		this.preparedStatement.setNull(paramIndex, sqlType, typeName);
	}

	public void setNull(int parameterIndex, int sqlType) throws SQLException {
		this.preparedStatement.setNull(parameterIndex, sqlType);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException {
		this.preparedStatement.setObject(parameterIndex, x, targetSqlType, scale);
	}

	public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException {
		this.preparedStatement.setObject(parameterIndex, x, targetSqlType);
	}

	public void setObject(int parameterIndex, Object x) throws SQLException {
		this.preparedStatement.setObject(parameterIndex, x);
	}

	public void setRef(int i, Ref x) throws SQLException {
		this.preparedStatement.setRef(i, x);
	}

	public void setShort(int parameterIndex, short x) throws SQLException {
		this.preparedStatement.setShort(parameterIndex, x);
	}

	public void setString(int parameterIndex, String x) throws SQLException {
		this.preparedStatement.setString(parameterIndex, x);
	}

	public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
		this.preparedStatement.setTime(parameterIndex, x, cal);
	}

	public void setTime(int parameterIndex, Time x) throws SQLException {
		this.preparedStatement.setTime(parameterIndex, x);
	}

	public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
		this.preparedStatement.setTimestamp(parameterIndex, x, cal);
	}

	public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
		this.preparedStatement.setTimestamp(parameterIndex, x);
	}

	@Deprecated
	public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {
		this.preparedStatement.setUnicodeStream(parameterIndex, x, length);
	}

	public void setURL(int parameterIndex, URL x) throws SQLException {
		this.preparedStatement.setURL(parameterIndex, x);
	}
}
